import tushare as ts
import sys

import logging as log

if len(sys.argv) < 3:
    print("./<cmd> code_begin code_end")
    sys.exit(0)

code_begin = sys.argv[1]
code_end = sys.argv[2]

basic_info_file = "stock_basic_list.csv"
code_list = []
latest_date = 20121130
file = open(basic_info_file, "r")
for i, line in enumerate(file):
    if i == 0:
        continue
    items = line.split(",")
    date = int(items[-1])
    if date > latest_date:
        continue
    code = items[0]
    code_list.append(code)

file.close()
num_codes = len(code_list)
print(num_codes)
code_list.sort()

#code_list = []
end_code = "000928"
date_begin = "2004-01-01"
date_end = "2015-11-30"
for i, code in enumerate(code_list):
    if code < code_begin or code >= code_end:
        continue
    print("processing code: ", code)
    #df = ts.get_hist_data(code)
    data = ts.get_h_data(code, autype=None, start=date_begin, end=date_end)
    print()
    if data.empty:
        log.error("fail to fetch code: %s", code)
        continue
    data.sort_index(inplace=True)
    fq = ts.get_h_data(code, autype="hfq", start=date_begin, end=date_end)
    print()
    if fq.empty:
        fq = ts.get_h_data(code, autype="qfq", start=date_begin, end=date_end)
        print()
        if fq.empty:
            log.error("missing fuquan data of code: %s", code)
            continue
    fq.sort_index(inplace=True)
    data["adj_close"] = fq["close"]
    data.to_csv("data/" + code + ".asc")
    print("progress %.2f%%" % (i * 100.0 / num_codes))